﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Basic4Net.BusinessRules;
using Basic4Net.Controls;
using Basic4Net.Entities;
using Basic4Net.OAWeb.Base;

namespace Basic4Net.OAWeb.Module_Report
{
    public partial class TeacherWageInNormalMonth : BasePage
    {
        private static Amount _amount;
        protected static Amount HAmount
        {
            get { return _amount ?? (_amount = new Amount()); }
        }

        private static UnSetWork _un;
        protected static UnSetWork HUnSetWork
        {
            get { return _un ?? (_un = new UnSetWork()); }
        }

        protected Dictionary<int, string> UnDic;
        protected IList<t_UnSetWork> UnWorkList;
        protected int PageType;
        protected bool IsAdmin;
        protected int UserId;
// ReSharper disable InconsistentNaming
        protected void Page_Load(object sender, EventArgs e)
// ReSharper restore InconsistentNaming
        {
            PageType = 0;
            if (!Int32.TryParse(Request.Params.Get("pagetype"), out PageType))
                PageType = 0;

            if (!Int32.TryParse(Session["UserID"].ToString(), out UserId))
                UserId = 0;
            IsAdmin = ExistSuper(UserId);
            if (IsAdmin)
                hidIsAdmin.Value = "1";
            else
                hidIsAdmin.Value = "0";

            UnDic = new Dictionary<int, string>();
            UnWorkList = HUnSetWork.FindAll();
            foreach (t_UnSetWork tUnSetWork in UnWorkList)
            {
                UnDic.Add(tUnSetWork.RID, tUnSetWork.WorkName);
            }

            if (IsPostBack)
                return;

            BindMonths();
            BindData(0, aspNetPager.PageSize);
        }

        protected void DrDateChanged(object sender, EventArgs e)
        {
            BindData(0, aspNetPager.PageSize);
        }

        /// <summary>
        /// 分页控件事件
        /// </summary>
        protected void AspNetPagerPageChanged(object sender, BNPager.PageChangedEventArgs e)
        {
            aspNetPager.PageIndex = e.NewPageIndex;
            BindData((e.NewPageIndex - 1) * aspNetPager.PageSize, aspNetPager.PageSize);
        }

        protected void BtnSearchClick(object sender, EventArgs e)
        {
            BindData((aspNetPager.PageIndex - 1) * aspNetPager.PageSize, aspNetPager.PageSize);
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="startAt">skip数据条数</param>
        /// <param name="step">取数据的条数</param>
        private void BindData(int startAt, int step)
        {
            int count = 0;
            int year;
            int month;
            string[] strarr = drDate.SelectedValue.Split(new[] { '-' });
            if (strarr.Length != 2)
                return;

            Int32.TryParse(strarr[0], out year);
            Int32.TryParse(strarr[1], out month);
            if (year < 1 || month < 1)
                return;

            IList<t_MeritPay> resultList = HAmount.GetAmountInfoByDate(startAt, step, year, month, SchoolMark, PageType, txtCode.Text, ref count, IsAdmin, UserId);
            aspNetPager.RecordCount = count;
            Repeater1.DataSource = resultList;
            Repeater1.DataBind();
        }

        private void BindMonths()
        {
            IList<string> yearList = HAmount.GetAmountMonth(SchoolMark, 0);
            if (yearList == null || yearList.Count < 1)
            {
                drDate.Items.Add(new ListItem("--当前没有拨款历史数据--", "-1"));
                return;
            }
            drDate.DataSource = yearList;
            //drDate.DataTextField = "String";
            //drDate.DataValueField = "Value";
            drDate.DataBind();
        }
    }
}